package com.gg.zjj.service.impl;

import com.gg.common.mapper.*;
import com.gg.common.pojo.*;
import com.gg.hj.util.INumber;
import com.gg.zjj.mapper.MManufactureViewMapper;
import com.gg.zjj.pojo.DataAfterSubmission;
import com.gg.zjj.pojo.LastProcessView1;
import com.gg.zjj.pojo.MManufactureView;
import com.gg.zjj.pojo.MProcedureModuleData;
import com.gg.zjj.service.InternalProductionAuditService;
import com.gg.zjj.util.Result;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author koco
 * 2022/10/25
 * 16:47
 */
@Service
public class InternalProductionAuditServiceImpl implements InternalProductionAuditService {

    @Resource
    private MManufactureMapper manufactureMapper;

    @Resource
    private MProcedureMapper procedureMapper;

    @Resource
    private MProcedureModuleMapper moduleMapper;

    @Resource
    private MProcedureIngMapper ingMapper;


    @Resource
    private MManufactureViewMapper manufactureViewMapper;

    @Resource
    private INumber iNumber;

    @Resource
    private SGatherMapper gatherMapper;

    @Resource
    private SGatherDetailsMapper detailsMapper;

    @Resource
    private DFileMapper fileMapper;


    @Override
    public List<MManufacture> queryTheProductionOperationToBeApprovedService() {
        return manufactureMapper.queryTheProductionOperationToBeApprovedMapper();
    }

    @Override
    public MManufacture queryingProductionProcessRecords(String val) {
        return manufactureMapper.queryingProductionProcessRecords(val);
    }

    @Override
    public MManufactureView queryAllIngProductionProcessRecords(String val) {
        return manufactureViewMapper.queryAllIngProductionProcessRecords(val);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Result approveOperationService(DataAfterSubmission dataAfterSubmission) {
        try {
            dataAfterSubmission.setCheckTime(dataAfterSubmission.getCheckTime().replace("T", " "));
//            MManufacture facture = manufactureMapper.queryByWorkOrderNumber(dataAfterSubmission.getWorkOrderNumber());
            procedureMapper.modifyActualQuantity(dataAfterSubmission);
//            dataAfterSubmission.setRealModuleCostPriceSum((int) (dataAfterSubmission.getRealModuleCostPriceSum() + facture.getRealModuleCostPriceSum()));
//            dataAfterSubmission.setRealLabourCostPriceSum((int) (dataAfterSubmission.getRealLabourCostPriceSum() + facture.getRealLabourCostPriceSum()));
            ingMapper.modifyAuditInformation(dataAfterSubmission);
            manufactureMapper.modifyActualQuantity(dataAfterSubmission);
            for (MProcedureModuleData mProcedureModuleData : dataAfterSubmission.getModelData()) {
                moduleMapper.modifyActualQuantity(mProcedureModuleData);
            }
        } catch (Exception e) {
            return Result.error("审核失败");
        }
        return Result.success("操作成功");
    }

    @Override
    public Result approvalProcessSubmissionService(String val) {
        procedureMapper.approvalProcessSubmissionMapper(val);
        return Result.success();
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void submitTheLastProcess(String val) throws Exception {
        //TODO 最后一道交接工序
        Collection collection = JSONArray.toCollection(JSONArray.fromObject(val));
        List list = new ArrayList(collection);
        SGather o1 = (SGather) JSONObject.toBean(JSONObject.fromObject(list.get(0)), SGather.class);
        o1.setRegisterTime(o1.getRegisterTime().replace("T", " "));
        SGatherDetails o2 = (SGatherDetails) JSONObject.toBean(JSONObject.fromObject(list.get(1)), SGatherDetails.class);

        LastProcessView1 o3 = (LastProcessView1) JSONObject.toBean(JSONObject.fromObject(list.get(2)), LastProcessView1.class);
        o3.setQualifiedQuantity(o1.getAmountSum() + "");
        manufactureMapper.lastStepModification(o3);
        procedureMapper.approvalProcessSubmissionMapper(o3.getProcedureId());
        o1.setGatherId(iNumber.number("401"));
        gatherMapper.insertSelective(o1);
        DFile file = fileMapper.selectByProcedureId(o3.getProductNumber());
        file.setRealCostPrice(o1.getCostPriceSum());
        file.setFileChangeAmount(file.getFileChangeAmount() +1);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        file.setPriceChangeTag("J001-1");
        file.setChangeTime(format.format(date));
        file.setChanger(o3.getChecker());
        fileMapper.modifyCostUnitPrice(file);
        //修改产品档案表
        o2.setParentId(o1.getId());
        o2.setAmountUnit(file.getAmountUnit());
        o2.setCostPrice(file.getListPrice());
        detailsMapper.insertSelective(o2);
    }
}
